package services;

import domain.entities.*;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: scamisay
 * Date: 12/2/12
 * Time: 1:47 AM
 * To change this template use File | Settings | File Templates.
 */
@Service
public class EquipmentSiteProtocoloCompromisoPaisService implements CRUDService<EquipmentSiteProtocoloCompromisoPais> {

    private EquipmentcertificationService equipmentcertificationService;
    private EquipmentSiteProtocoloCompromisoPaisDAO dao;


    public EquipmentSiteProtocoloCompromisoPaisService(){}

    @Autowired
    public EquipmentSiteProtocoloCompromisoPaisService(EquipmentSiteProtocoloCompromisoPaisDAO dao, EquipmentcertificationService equipmentcertificationService){
        this.dao = dao;
        this.equipmentcertificationService = equipmentcertificationService;
    }

    @Override
    public <V extends Criteria> List<EquipmentSiteProtocoloCompromisoPais> list(V criteria) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public void save(EquipmentSiteProtocoloCompromisoPais equipmentSiteProtocoloCompromisoPais) {

    }

    public List<EquipmentSiteProtocoloCompromisoPais> getList(Centro site, ProtocoloCompromisopais pcp){


        List<EquipmentSiteProtocoloCompromisoPais> output = new ArrayList<EquipmentSiteProtocoloCompromisoPais>(0);
        List<Equipmentcertification> list = equipmentcertificationService.findByCentroId(site.getId());
        Map<Equipmentcertification,EquipmentSiteProtocoloCompromisoPais> map = getEquipmentMapBySite(site);


        Iterator<Equipmentcertification> it =  list.iterator();
        while(it.hasNext()){
            Equipmentcertification e = it.next();
            EquipmentSiteProtocoloCompromisoPais espcp;
            if(!map.containsKey(e)){
                espcp = new EquipmentSiteProtocoloCompromisoPais();
                espcp.setEquipment(e);
                espcp.setPcp(pcp);
                espcp.setSite(site);

                output.add(espcp);
            }else{
                espcp = map.get(e);
                output.add(espcp);
            }
        }
        //output = dao.findByCentroId(site.getId());
        return output;
    }

    private Map<Equipmentcertification,EquipmentSiteProtocoloCompromisoPais> getEquipmentMapBySite(Centro site){
        Set<EquipmentSiteProtocoloCompromisoPais> set = new HashSet<EquipmentSiteProtocoloCompromisoPais>(
                dao.findByCentroId(site.getId())
        );
        Map<Equipmentcertification,EquipmentSiteProtocoloCompromisoPais> map =
                new HashMap<Equipmentcertification, EquipmentSiteProtocoloCompromisoPais>(0);

        for(EquipmentSiteProtocoloCompromisoPais e : set){
            map.put(e.getEquipment(),e);
        }

        return map;
    }

    public void save(List<EquipmentSiteProtocoloCompromisoPais> list, ProtocoloCompromisopais protocoloCompromisopais, Centro site) {
        for(EquipmentSiteProtocoloCompromisoPais e : list){
            doSave(e,protocoloCompromisopais,site);
        }
    }

    private void doSave(EquipmentSiteProtocoloCompromisoPais e ,  ProtocoloCompromisopais protocoloCompromisopais, Centro site){
        e.setPcp(protocoloCompromisopais);
        e.setSite(site);
        dao.merge(e);
    }

    private Date parseDate(String strDate){
        DateTimeFormatter parser = DateTimeFormat.forPattern("dd/MMM/yy");
        DateTime time = parser.parseDateTime(strDate);

        Calendar c = time.toGregorianCalendar();
        return new Date(c.getTimeInMillis());
    }

    private String printDate(Date date){
        DateTime dt = new DateTime(date);
        DateTimeFormatter format = DateTimeFormat.forPattern("dd/MMM/yy");
        return format.print(dt);
    }
}
